<?php
class bond
{
	/*
	 *	type = true for follow
	 *	type = false for unfollow
	*/
	function dealBondUser($user_id, $type)
	{
		$type = $type ? 1 : 0;
		$user = user::getById($user_id);
		$current_user_id = user::getId();
		
		if (empty($user_id) || empty($user)
			|| empty($current_user_id)
			|| $current_user_id==$user_id)
		{
			return false;
		}
		
		if ($user_id < $current_user_id) {
			$sql = sys::getSqlString("insert into user_bonds (user_a_id, user_b_id, b_to_a, create_time_ba)"
				." values ('%d', '%d', '%d', '%s') on duplicate key update b_to_a='%d';",
				$user_id, $current_user_id, $type, date('Y-m-d H:i:s'), $type);
		} else {
			$sql = sys::getSqlString("insert into user_bonds (user_a_id, user_b_id, a_to_b, create_time_ab)"
				." values ('%d', '%d', '%d', '%s') on duplicate key update a_to_b='%d';",
				$current_user_id, $user_id, $type, date('Y-m-d H:i:s'), $type);
		}
		
		return sys::sql($sql);
	}
	
	function followUser($user_id)
	{
		return self::dealBondUser($user_id, true);
	}

	function unfollowUser($user_id)
	{
		return self::dealBondUser($user_id, false);
	}
	
	function getUserFolloweesId($user_id)
	{
		if (!$user_id) return array();
		$retarr = array();
		
		$result = sys::sql("select bond_id, user_b_id as user_id from user_bonds".
			" where user_a_id='%d' and a_to_b=1",
			$user_id);
		for ($i=0; $i<count($result); $i++)
		{
			$retarr[$result[$i]['bond_id']] = $result[$i]['user_id'];
		}
		
		$result = sys::sql("select bond_id, user_a_id as user_id from user_bonds".
			" where user_b_id='%d' and b_to_a=1",
			$user_id);
		for ($i=0; $i<count($result); $i++)
		{
			$retarr[$result[$i]['bond_id']] = $result[$i]['user_id'];
		}
		
		if (count($retarr) > 0)
		{
			krsort($retarr);
			return array_values($retarr);
		}
		else
		{
			return array();
		}
	}
	
	function getUserFollowersId($user_id)
	{
		if (!$user_id) return array();
		$retarr = array();
		
		$result = sys::sql("select bond_id, user_b_id as user_id from user_bonds".
			" where user_a_id='%d' and b_to_a=1",
			$user_id);
		for ($i=0; $i<count($result); $i++)
		{
			$retarr[$result[$i]['bond_id']] = $result[$i]['user_id'];
		}
		
		$result = sys::sql("select bond_id, user_a_id as user_id from user_bonds".
			" where user_b_id='%d' and a_to_b=1",
			$user_id);
		for ($i=0; $i<count($result); $i++)
		{
			$retarr[$result[$i]['bond_id']] = $result[$i]['user_id'];
		}
		
		if (count($retarr) > 0)
		{
			krsort($retarr);
			return array_values($retarr);
		}
		else
		{
			return array();
		}
	}

	function isFollowing($user_id)
	{
		$type = $type ? 1 : 0;
		$user = user::getById($user_id);
		$current_user_id = user::getId();
		
		if (empty($user_id) || empty($user)
			|| empty($current_user_id)
			|| $current_user_id==$user_id)
		{
			return -1;
		}
		
		if ($user_id < $current_user_id) {
			$sql = sys::getSqlString("select b_to_a as bond from user_bonds".
				" where user_a_id='%d' and user_b_id='%d'",
				$user_id, $current_user_id);
		} else {
			$sql = sys::getSqlString("select a_to_b as bond from user_bonds".
				" where user_b_id='%d' and user_a_id='%d'",
				$user_id, $current_user_id);
		}
		
		$result = sys::sql($sql);
		return intval($result[0]['bond'])==1;
	}

}